package com.demo.web;

import com.demo.bean.PositionRequest;
import com.demo.bean.PositionVO;
import com.demo.service.AdsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

/**
 * 一个简单的Jersey资源类
 */

@RequestMapping("/")
@RestController
public class SampleController {

    private final AdsService adsService;

    /* 构造注入 */
    private SampleController(@Autowired AdsService adsService) {
        this.adsService = adsService;
    }

    @RequestMapping(value = "/ads", method = RequestMethod.POST)
    public PositionVO getAds(@RequestHeader Map<String, String> headers, @RequestBody PositionRequest positionReq) {
        String authorization = headers.get("authorization");
        System.out.println("authorization: " + authorization);
        return adsService.getAds(positionReq);
    }

    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public Map<Object, Object> post() {
        return Map.of("company", "亿贝", "location", "san jose", "employee", "10000");
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get() {
        return "Hello World";
    }

    /**
     * @param name path parameter不能用中文，否则400，目前还没解决
     */
    @RequestMapping(value = "/get_with_path/{name}", method = RequestMethod.GET)
    public String pathParamGet(@PathVariable("name") String name) {
        return "path param name: " + Optional.ofNullable(name).orElse("sophia");
    }

    @RequestMapping(value = "/get_with_query", method = RequestMethod.GET)
    public String queryParamGet(@RequestParam String location) {
        return "query param location: " + Optional.ofNullable(location).orElse("dalian");
    }

}
